﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.DependencyInjection;
using StyleMVVM.UnitTests.TestClasses.ValidationObjects;
using StyleMVVM.Validation;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

#else
using Microsoft.VisualStudio.TestTools.UnitTesting;

#endif

namespace StyleMVVM.UnitTests.Validation
{
	[TestClass]
	public class ObservableCollectionValidationTests : BaseTestClass
	{
		[TestMethod]
		public void BasicListTest()
		{
			IValidationService validationService = Container.Locate<IValidationService>();
			ObservableCollectionValidationObject collection = new ObservableCollectionValidationObject();

			IValidationContext context = validationService.GetValidationContext(collection);

			Assert.AreEqual(ValidationState.Valid, context.State);

			collection.SimpleList = new ObservableCollection<SimpleValidationObject>();

			Assert.AreEqual(ValidationState.Valid, context.State);

			collection.SimpleList.Add(new SimpleValidationObject());

			Assert.AreEqual(ValidationState.Invalid, context.State);
			Assert.AreEqual(3, context.RuleResults().Count());

			collection.SimpleList[0].Name = "hello";
			collection.SimpleList[0].TestInt = 4;

			Assert.AreEqual(ValidationState.Valid, context.State);
		}

		[TestMethod]
		public void AddRemoveListTest()
		{
			IValidationService validationService = Container.Locate<IValidationService>();
			ObservableCollectionValidationObject collection = new ObservableCollectionValidationObject();

			IValidationContext context = validationService.GetValidationContext(collection);

			Assert.AreEqual(ValidationState.Valid, context.State);

			collection.SimpleList = new ObservableCollection<SimpleValidationObject>();

			Assert.AreEqual(ValidationState.Valid, context.State);

			for (int i = 0; i < 30; i++)
			{
				collection.SimpleList.Add(new SimpleValidationObject());
			}

			Assert.AreEqual(ValidationState.Invalid, context.State);
			Assert.AreEqual(90, context.RuleResults().Count());

			while (collection.SimpleList.Count > 0)
			{
				collection.SimpleList.RemoveAt(0);

				Assert.AreEqual(collection.SimpleList.Count * 3, context.RuleResults().Count());
			}

			Assert.AreEqual(ValidationState.Valid, context.State);
		}

		[TestMethod]
		public void ComplexListTest()
		{
			IValidationService validationService = Container.Locate<IValidationService>();
			ObservableCollectionValidationObject collection = new ObservableCollectionValidationObject();

			IValidationContext context = validationService.GetValidationContext(collection);

			Assert.AreEqual(ValidationState.Valid, context.State);

			collection.SimpleList = new ObservableCollection<SimpleValidationObject>();

			Assert.AreEqual(ValidationState.Valid, context.State);

			for (int i = 0; i < 30; i++)
			{
				collection.SimpleList.Add(new SimpleValidationObject());
			}

			Assert.AreEqual(ValidationState.Invalid, context.State);
			Assert.AreEqual(90, context.RuleResults().Count());

			for (int i = 0; i < 30; i++)
			{
				collection.SimpleList[i].Name = "Hello";
				collection.SimpleList[i].TestInt = 2;

				Assert.AreEqual((29 - i) * 3, context.RuleResults().Count());
			}

			Assert.AreEqual(ValidationState.Valid, context.State);
		}
	}
}